//
//  LSZWalkmanDetailsViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/25.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZWalkmanDetailsViewController.h"

@interface LSZWalkmanDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewWalkman;
@property(nonatomic,strong) NSMutableArray * arrayData;
@property(nonatomic,assign) NSInteger page;
@end

@implementation LSZWalkmanDetailsViewController
#pragma mark - 获取数据
-(void)getDataStudyPlan:(NSInteger)page
{
    [LSZRequestDicData secondaryInterfaceForData:self.id_id page:page:^(BOOL isOK, NSString * string, LSZSecondaryInterfaceManager * mg) {
        if (isOK == YES) {
            if(page == 0){
                [self.arrayData removeAllObjects];
                [self.arrayData addObjectsFromArray:mg.arrayData];
            }else{
                [self.arrayData addObjectsFromArray:mg.arrayData];
            }
            [self.tableViewWalkman reloadData];
        }else{
            if ([string isEqualToString:@"重复登录"]) {
                [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZWalkmanDetailsViewController"];
            }else{
                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
            }
        }
    }];
}
-(void)goToLogin
{
    [LSZPublicClassDataToll goToLogin];
}
-(void)addNotificationCenter
{
    NSString * strDelete = [NSString stringWithFormat:@"%@Login",@"LSZWalkmanDetailsViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToLogin) name:strDelete object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    [self addNotificationCenter];
    weakSelf.arrayData = [[NSMutableArray alloc]init];
    weakSelf.page = 0;
    [weakSelf getDataStudyPlan:weakSelf.page];
    [weakSelf.tableViewWalkman registerClass:[LSZWalkmanCategoryTableViewCell class] forCellReuseIdentifier:@"LSZWalkmanCategoryTableViewCell"];
    /*
    [weakSelf.tableViewWalkman addRefreshHeaderWithHandle:^{
        NSLog(@"开始刷新");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page = 0;
            [weakSelf getDataStudyPlan:weakSelf.page];
            [weakSelf.tableViewWalkman.mj_header endRefreshing];
        });
    }];
    
    weakSelf.tableViewWalkman.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.page++;
            [self getDataStudyPlan:self.page];
            [self.tableViewWalkman.mj_footer endRefreshing];
        });
    }];
    */
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZWalkmanCategoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZWalkmanCategoryTableViewCell" forIndexPath:indexPath];
    if (self.arrayData.count > indexPath.row) {
        LSZSecondaryInterfaceMg * mg = nil;
        mg =self.arrayData[indexPath.row];
        cell.secondaryInterfaceMg = mg;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.arrayData.count > indexPath.row) {
        LSZSecondaryInterfaceMg * mg = nil;
        mg =self.arrayData[indexPath.row];
        LSZAudioDetailsViewController * vc = [[LSZAudioDetailsViewController alloc]init];
        vc.id_id = mg.id_id;
        vc.name = mg.name;
        vc.summary = mg.summary;
        vc.coverUrl = mg.coverUrl;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(UITableView *)tableViewWalkman
{
    if (_tableViewWalkman == nil) {
        _tableViewWalkman = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , [[UIScreen mainScreen] bounds].size.height-TAB_BAR_HEIGHT-10) style:UITableViewStyleGrouped];
        _tableViewWalkman.delegate = self;
        _tableViewWalkman.dataSource = self;
        _tableViewWalkman.estimatedRowHeight=0;
        _tableViewWalkman.estimatedSectionFooterHeight =0;
        _tableViewWalkman.estimatedSectionHeaderHeight =0;
        [_tableViewWalkman setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableViewWalkman];
    }
    return _tableViewWalkman;
}

@end
